package airAgent.group2;

import java.util.HashMap;

import airAgent.group2.util.Util;

import ontology.ontology.AirAgentOntology;
import ontology.ontology.CreateAccount;
import ontology.ontology.Deposit;
import ontology.ontology.Withdrawn;

import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.UngroundedException;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;


public class Bank extends Agent {
	private Codec codec = new SLCodec();
	private Ontology ontology = AirAgentOntology.getInstance();

	private static final long serialVersionUID = 1L;
	private HashMap<String, Float> accs = new HashMap<String, Float>();

	protected void setup() {

		// Register of the codec and the ontology to be used in the
		// ContentManager
		getContentManager().registerLanguage(codec);
		getContentManager().registerOntology(ontology);

		Util.register(this.getName(), Util.BANK, this);

		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = 1L;

			public void action() {
				ACLMessage msg = receive(MessageTemplate.and(
						MessageTemplate.MatchLanguage(codec.getName()),
						MessageTemplate.MatchOntology(ontology.getName())));
				if (msg != null) {
					ContentElement ce = null;
					if (msg.getPerformative() == ACLMessage.REQUEST) {
						try {
							ce = getContentManager().extractContent(msg);
						} catch (UngroundedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (CodecException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (OntologyException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						if (ce instanceof Action) {
							Action agAction = (Action) ce;
							Concept con = agAction.getAction();
							
							ACLMessage reply = msg.createReply();
							reply.setLanguage(codec.getName());
							reply.setOntology(ontology.getName());
							
							
							String sender = msg.getSender().getLocalName();

							if (con instanceof Deposit) {
								float amount = ((Deposit) con).getAmount().getValue();
								//System.out.println(myAgent.getLocalName()+ " Depositing "+ amount);
								/*Deposit ca = new Deposit();
								Money m = new Money();
								m.setValue(0.0F);
								ca.setAmount(m);
								Action agActionResponse = new Action(
										msg.getSender(), ca);
								try {
									getContentManager().fillContent(reply,
											agActionResponse);
								} catch (CodecException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (OntologyException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}*/
								reply.setProtocol(AirAgentOntology.DEPOSIT);
								if (accs.containsKey(sender)) {
									accs.put(sender, accs.get(sender) + amount);
									System.out.println(sender + "'s balance is now " + accs.get(sender));
									reply.setPerformative(ACLMessage.INFORM);
								} else {
									reply.setPerformative(ACLMessage.REFUSE);
								}
								myAgent.send(reply);
							}
							if (con instanceof Withdrawn) {
								float amount = ((Withdrawn) con).getAmount().getValue();
								/*Withdrawn ca = new Withdrawn();
								Money m = new Money();
								m.setValue(0.0F);
								ca.setAmount(m);
								Action agActionResponse = new Action(
										msg.getSender(), ca);
								try {
									getContentManager().fillContent(reply,
											agActionResponse);
								} catch (CodecException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (OntologyException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}*/
								reply.setProtocol(AirAgentOntology.WITHDRAWN);
								if (accs.containsKey(sender)) {
									accs.put(sender, accs.get(sender) - amount);
									System.out.println(sender + "'s balance is now " + accs.get(sender));
									reply.setPerformative(ACLMessage.INFORM);
								} else {
									reply.setPerformative(ACLMessage.REFUSE);
								}
								myAgent.send(reply);
							}
							if (con instanceof CreateAccount) {
								/*System.out.println(myAgent.getLocalName()
										+ " Creating account for "
										+ msg.getSender().getLocalName());
								CreateAccount ca = new CreateAccount();
								Action agActionResponse = new Action(
										msg.getSender(), ca);
								try {
									getContentManager().fillContent(reply,
											agActionResponse);
								} catch (CodecException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (OntologyException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}*/
								reply.setProtocol(AirAgentOntology.CREATEACCOUNT);
								if (!accs.containsKey(sender)) {
									accs.put(sender, 0.0F);
									reply.setPerformative(ACLMessage.INFORM);
								} else {
									reply.setPerformative(ACLMessage.REFUSE);
								}
								myAgent.send(reply);
							}
						}
					}
				}

			}
		});

	}
}
